use std::fmt;

use serde::{Deserialize, Serialize};

#[derive(Debug, Default, Serialize, Deserialize)]
pub struct Empty {}

impl fmt::Display for Empty {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", "{}")
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Resp<T> {
    pub code: i32,
    pub message: String,
    pub data: T,
}

impl<T> Resp<T> {
    pub fn new(code: i32, message: String, data: T) -> Resp<T> {
        Resp {
            code,
            message,
            data,
        }
    }
    pub fn success(data: T) -> Resp<T> {
        Resp::new(200, "success".to_string(), data)
    }
    pub fn exception(msg: String, data: T) -> Resp<T>
    where
        T: Serialize,
    {
        Resp::new(-1, msg, data)
    }
}

impl<T> fmt::Display for Resp<T>
where
    T: fmt::Display + Serialize,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", serde_json::json!(self))
    }
}
